<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CommonController;
use App\Models\shop\Product;
use App\Models\shop\Brand;
use App\Models\shop\goodscategory;
use App\Models\shop\product_gallery;


class ProductController extends CommonController
{

    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_system' => 'shop',
            '_product' => 'am-active',

        ]);
    }


    private function attributes()
    {
        view()->share([
            'brands'=>Brand::orderBy('sort_order')->get(),
            'categories'=>goodscategory::filter_categories(),
            'products'=>Product::get(),
        ]);
    }
    
    //显示商品列表
    public function index(Request $request)
    {
        /*if($request->has('name')){
            echo $request->name;
        }*/

    /*    if($request->has('created_at') && $request->created_at!='')
        {
            $time=explode('~',$request->created_at);
            return $time;
        }*/

        //多条件查找
        $where=function($query) use ($request){
            //过滤名字
            if($request->has('name')&&$request->name!='')
            {
                $name= "%" . $request->name . "%";
                $query->where('name','like',$name);
            }
            //过滤分类
            if($request->has('category_id') && $request->category_id !='-1')
            {
                $category_id=$request->category_id;
                $query->where('category_id',$category_id);
            }
            //过滤品牌
            if($request->has('brand_id') && $request->brand_id !='-1')
            {
                $brand_id=$request->brand_id;
                $query->where('brand_id',$brand_id);
            }

            //置顶
            if($request->has('is_top'))
            {
                $query->where('is_top',true);
            }
            //推荐
            if($request->has('is_recommend'))
            {
                $query->where('is_recommend',true);
            }
            //热销
            if($request->has('is_hot'))
            {
                $query->where('is_hot',true);
            }
            //新品
            if($request->has('is_new'))
            {
                $query->where('is_new',true);
            }

            //上架 下架
            if($request->has('is_onsale') && $request->is_onsale!='-1')
            {
                $query->where('is_onsale',$request->is_onsale);
            }

            //时间过滤
            if($request->has('created_at') && $request->created_at!=' ')
            {
                $time=explode('~',$request->created_at); //$time是一个数组
                $time['0']=$time['0'].' 00:00:00';
                $time['1']=$time['1'].' 23:59:59';
                $query->whereBetween('created_at',$time);
            }
        };

        $this->attributes();
        $products=Product::with('brand')
            ->with('category')
            ->orderBy('is_top','desc')
            ->where($where)
            ->orderBy('created_at', "desc")
            ->paginate(config('fangshop.page_size'));
//        return $products;exit;
        return view('admin.shop.products.index')->with('products',$products);
    }

    //显示添加
    public function create()
    {
//        $this->attributes();
        $categories=goodscategory::get_categories();
        $brands=Brand::get();
        return view('admin.shop.products.create')->with('categories',$categories)->with('brands',$brands)->with(['_new_product' => 'am-active','_product'=>'']);
    }
    //执行添加
    public function store(Request $request)
    {

        $data=$request->except(['stock','brand_id','file','imgs']);
        $data['stock']=$request->stock=='无限'?'-1':$request->stock;
        $data['brand_id']=$request->brand_id=='-1'?'':$request->brand_id;

        $product=Product::create($data);  //$product是个对象


        //相册
        if($request->has('imgs')){
            foreach($request->imgs as $img){
                $product->product_gallery()->create(['img'=>$img]);  //能自动给product_id赋值？

            }
        }

        return redirect(route('shop.product.index'))->with('success111', '新增成功~');
    }

    //编辑
    public function edit($id)
    {
        $this->attributes();
        $product=Product::with('product_gallery')->find($id);
        return view('admin.shop.products.edit')->with('product',$product);
    }

    //更新
    public function update(Request $request,$id)
    {
//        return $request->all();
        $data=$request->except(['stock','brand_id','file','imgs']);
        $data['stock']=$request->stock=='无限'?'-1':$request->stock;
        $data['brand_id']=$request->brand_id=='-1'?'':$request->brand_id;

        $product=Product::find($id);  //$product是个对象
        $product->update($data);


        //相册
        if($request->has('imgs')){
            foreach($request->imgs as $img){
                $product->product_gallery()->create(['img'=>$img]);  //能自动给product_id赋值？

            }
        }

        return redirect(route('shop.product.index'))->with('success', '更新成功~');

    }
    //删除product
    public function destroy($id)
    {
        Product::destroy($id);
        return redirect(route('shop.product.index'))->with('success', '删除成功~');
    }

    //多选删除
    public function destroy_checked(Request $request)
    {
        $ids=$request->ids; //$ids是个字符串
        $ids=explode(' ',trim($ids));
        foreach ($ids as $id){
            Product::destroy($id);
        }

    }


    //显示回收站
    public function trash()
    {
        $products=Product::with('brand')
            ->with('category')
            ->onlyTrashed()  // withTrashed()
            ->orderBy('deleted_at', "desc")
            ->paginate(config('fangshop.page_size'));

//        return $products;exit;

        return view('admin.shop.products.trash')->with('products',$products)->with(['_trash'=>'am-active','_product'=>'']);
    }

    //回收站还原单个
    public function restore($id)
    {
        Product::withTrashed()->where('id', $id)->restore();
        return back();
    }
    //回收站还原多个
    public function restore_checked(Request $request)
    {
        $ids=$request->ids; //$ids是个字符串
        $ids=explode(' ',trim($ids));
        foreach ($ids as $id){
            Product::withTrashed()->where('id', $id)->restore();
        }
//        return back();
    }

    //回收站强制删除
    public function delete_force($id)
    {
        $product=Product::withTrashed()->find($id);
        $product->forceDelete();
        return back();
    }

    //删除destroy_gallery
    public function destroy_gallery(Request $request){
        product_gallery::destroy($request->gallery_id);
    }

    //ajax修改属性
    public function is_something(Request $request)
    {
        $attr=$request->attr;
        $id=$request->id;

        $product=Product::find($id);
        $product->$attr=$product->$attr?false:true;
        $product->save();
    }

    //ajax修改库存
    public function stock(Request $request)
    {
        $stock=$request->stock;
        $id=$request->id;

        $product=Product::find($id);
        $product->stock=$stock;
        $product->save();
    }


}
